package com.banciyuan.bcywebview.biz.post.write.series;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.RadioButton;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.api.NovelApi;
import com.banciyuan.bcywebview.base.helper.ProgressbarHelper;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.view.toast.MyToast;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import com.banciyuan.bcywebview.utils.http.BcyPair;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import de.greenrobot.daoexample.model.CreateSeriesItem;

/**
 * Created by squall on 2016/2/22.
 */
public class SeriesEditActivity extends CreateSeriesActivity {

    private String wns_id;

    private RadioButton ing_rb;
    private RadioButton done_rb;

    protected ProgressbarHelper mProgressbarHelper;
    protected View mProgressbarView;

    @Override
    protected void initArgs() {
        super.initArgs();
        wns_id = getIntent().getStringExtra(HttpUtils.WNS_ID);
        hideAuto = true;
    }

    @Override
    protected void initProgressbar() {
        mProgressbarView = findViewById(R.id.base_progressbar);
        mProgressbarHelper = new ProgressbarHelper(mProgressbarView);
        mProgressbarHelper.setActionCallbacks(new ProgressbarHelper.OnProgressCallbacks() {
            @Override
            public void onActionClick(int id) {
                initData();
            }
        });
        mProgressbarHelper.onLoading();
    }

    @Override
    protected void initActionbar() {
        super.initActionbar();
        mActionbarHelper.setTitle(getString(R.string.edit_serise));
    }

    @Override
    protected void initData() {
        String url = HttpUtils.BASE_URL + NovelApi.getEditNovelSet();
        List<BcyPair> pairs = new ArrayList<BcyPair>();
        pairs.add(new BcyPair(HttpUtils.WNS_ID, wns_id));
        pairs.add(new BcyPair(HttpUtils.USER_TOKEN, UserDataHelper.getInstance(SeriesEditActivity.this).getToken()));
        HashMap<String, String> map = HttpUtils.getData(pairs);

        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    if (StatusCodeUtils.dealStatusCode(s, SeriesEditActivity.this)) {
                        JSONObject jsonObject = new JSONObject(s);
                        String result = jsonObject.getString(HttpUtils.UPLOAD_DATA);
                        Gson gson = new Gson();
                        seriesItem = gson.fromJson(result, new TypeToken<CreateSeriesItem>() {
                        }.getType());
                        renderData();
                    }
                } catch (Exception e) {
                    mProgressbarHelper.onFailed();
                }
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                MyToast.show(SeriesEditActivity.this, getString(R.string.modify_fail));
                if (!SeriesEditActivity.this.isFinishing()) {
                    mDialog.dismiss();
                }
                mProgressbarHelper.onFailed();
            }
        };
        StringRequestParam stringRequestParam = new StringRequestParam(Request.Method.POST, url, map, onSucc, onError);
        mQueue.add(stringRequestParam);
    }

    private void renderData() {
        if (seriesItem != null) {
            if (!TextUtils.isEmpty(seriesItem.getTitle())) {
                etSeriesTitle.setText(seriesItem.getTitle());
            }
            if (!TextUtils.isEmpty(seriesItem.getWork())) {
                etSeriesWork.setText(seriesItem.getWork());
            }

            if (!TextUtils.isEmpty(seriesItem.getType())) {
                if (seriesItem.getType().equals(dongjin)) {
                    rb_one.setChecked(true);
                } else {
                    rb_two.setChecked(true);
                }
            }
            if (!seriesItem.getSet_status().equals("end")) {
                ing_rb.setChecked(true);
            } else {
                done_rb.setChecked(true);
            }
        }

        mProgressbarHelper.onSuccess();
    }

    @Override
    protected void initUi() {
        super.initUi();
        findViewById(R.id.edit_line).setVisibility(View.VISIBLE);
        ing_rb = (RadioButton) findViewById(R.id.radio_ing);
        done_rb = (RadioButton) findViewById(R.id.radio_done);
    }


    private void commit() throws Exception {
        mDialog.show();
        String url = HttpUtils.BASE_URL + NovelApi.getDoEditNovelSet();
        List<BcyPair> pairs = new ArrayList<BcyPair>();
        pairs.add(new BcyPair(HttpUtils.WNS_ID, wns_id));
        pairs.add(new BcyPair(HttpUtils.USER_TOKEN, UserDataHelper.getInstance(SeriesEditActivity.this).getToken()));
        pairs.add(new BcyPair(HttpUtils.UPLOAD_DATA, getJson()));
        HashMap<String, String> map = HttpUtils.getData(pairs);

        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                if (!SeriesEditActivity.this.isFinishing()) {
                    mDialog.dismiss();
                }
                try {
                    if (StatusCodeUtils.dealStatusCode(s, SeriesEditActivity.this)) {
                        finish();
                    }
                } catch (Exception e) {
                    MyToast.show(SeriesEditActivity.this, getString(R.string.modify_fail));
                }
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                MyToast.show(SeriesEditActivity.this, getString(R.string.modify_fail));
                if (!SeriesEditActivity.this.isFinishing()) {
                    mDialog.dismiss();
                }
            }
        };
        StringRequestParam stringRequestParam = new StringRequestParam(Request.Method.POST, url, map, onSucc, onError);
        mQueue.add(stringRequestParam);

    }

    private String getJson() {
        Gson gson = new Gson();
        seriesItem.setWork(etSeriesWork.getText().toString().trim());
        seriesItem.setCover(writeCoverList.get(selectCoverIndex).getCover());
        seriesItem.setSet_status(ing_rb.isChecked() ? "ing" : "end");
        seriesItem.setTitle(etSeriesTitle.getText().toString().trim());
        return gson.toJson(seriesItem);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.ll_info_set:
                intent.setClass(SeriesEditActivity.this, SeriesInfoActivity.class);
                intent.putExtra(gotoUtil.INTENT_VALUE_ONE, seriesItem);
                startActivityForResult(intent, GO_SETINFO);
                break;
            case R.id.base_action_bar_home_text_item:
                try {
                    commit();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

}
